package com.xoa.service.equipment.impl;


import com.xoa.dao.equipment.LimsEquipFileMapper;
import com.xoa.dao.equipment.LimsEquipPartsMapper;
import com.xoa.model.equipment.LimsEquipFile;
import com.xoa.model.equipment.LimsEquipParts;
import com.xoa.model.users.Users;
import com.xoa.service.equipment.EquipmentAcceService;
import com.xoa.util.LimsJson;
import com.xoa.util.UserUtil;
 import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @作者 王穗穗
 * @创建日期 13:27 2020/1/8
 * @类介绍
 */
@Service
public class EquipmentAcceServiceImpl implements EquipmentAcceService {

    @Autowired
    private LimsEquipPartsMapper limsEquipPartsMapper;

    @Autowired
    private LimsEquipFileMapper limsEquipFileMapper;

    @Override
    public LimsJson EquipAccessItems(String equipNo,Integer equipId) {
        LimsJson limsJson=new LimsJson();
        String equipAccessNo=equipNo+"-";  //设备配件编码
        LimsEquipParts limsEquipParts=limsEquipPartsMapper.getEquipAccessNo(equipNo,equipId);
        if(limsEquipParts!=null&&limsEquipParts.getPartNo()!=null){
            String patrNo="";
            int number = Integer.parseInt(limsEquipParts.getPartNo());
            number = number+1;
            patrNo = String.valueOf(number);
            int length = patrNo.length();
            if(length<4){
                for (;;) {
                    patrNo = "0"+patrNo;
                    if(patrNo.length()==3){
                        break;
                    }
                }
            }
            equipAccessNo+=patrNo;
        }else{
            equipAccessNo+="001";
        }
        limsJson.setObj(equipAccessNo);
        return limsJson;
    }

    @Override
    public LimsJson getEquipAccess(Integer equipId, Integer page, Integer limit) {
        LimsJson limsJson=new LimsJson(1,"err");
        PageParams pageParams = new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        Map map=new HashMap();
        map.put("page", pageParams);
        map.put("equipId",equipId);
        List<LimsEquipParts> limsEquipParts=limsEquipPartsMapper.selectByEquipId(map);
        if(limsEquipParts!=null){
            limsJson.setCode(0);
            limsJson.setData(limsEquipParts);
            limsJson.setCount(pageParams.getTotal());
            limsJson.setMsg("成功");
        }else{
            limsJson.setMsg("系统错误，请联系管理员");
        }
        return limsJson;
    }

    @Override
    public LimsJson operationEquipAccess(LimsEquipParts limsEquipParts) {
        LimsJson limsJson=new LimsJson(1,"err");
        int count=0;
        Date date=newNextInatallTime(limsEquipParts.getInstallTime(),limsEquipParts.getChangeTimes());
        limsEquipParts.setNextInstallTime(date);    //获取下次更换设备配件时间
        if(limsEquipParts.getPartId()!=null){
            count=limsEquipPartsMapper.updateByPrimaryKeySelective(limsEquipParts);
        }else{
            count=limsEquipPartsMapper.insertSelective(limsEquipParts);
        }
        if(count>0){
            limsJson.setCount(0);
            limsJson.setMsg("操作成功");
        }else{
            limsJson.setMsg("无法对配件进行操作，请核实数据是否输入正确");
        }
        return limsJson;
    }

    @Override
    public LimsJson delEquipAccess(String partIds) {
        LimsJson limsJson=new LimsJson();
        String[] partId=partIds.split(",");
        int count=0;
        for(String pid:partId){
            count= limsEquipPartsMapper.deleteByPrimaryKey(Integer.parseInt(pid));
        }
        if (count>0){
            limsJson.setMsg("删除成功");
        }else{
            limsJson.setMsg("无法删除，请联系管理员");
        }
        return limsJson;
    }

    @Override
    public LimsJson getEquiFile(Integer equipId, Integer page, Integer limit) {
        LimsJson limsJson=new LimsJson(1,"err");
        PageParams pageParams = new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        Map map=new HashMap();
        map.put("page", pageParams);
        map.put("equipId",equipId);
        List<LimsEquipFile> limsEquipFiles=limsEquipFileMapper.selectByEquipId(map);
        if(limsEquipFiles!=null){
            limsJson.setCode(0);
            limsJson.setData(limsEquipFiles);
            limsJson.setCount(pageParams.getTotal());
            limsJson.setMsg("ok");
        }else{
            limsJson.setMsg("无法查询");
        }
        return limsJson;
    }

    @Override
    public LimsJson operationEquiFile(HttpServletRequest request, LimsEquipFile limsEquipFile) {
        LimsJson limsJson=new LimsJson(1,"err");
        int count=0;
        if(limsEquipFile.getFileId()!=null){
            count=limsEquipFileMapper.updateByPrimaryKeySelective(limsEquipFile);
        }else{
            Users users= UserUtil.getUser(request);
            limsEquipFile.setUpdateNmae(users.getUserName());
            limsEquipFile.setUpdateTime(new Date());
            count=limsEquipFileMapper.insertSelective(limsEquipFile);
        }
        if(count>0){
            limsJson.setCode(0);
            limsJson.setMsg("操作成功");
        }else{
            limsJson.setMsg("无法操作");
        }
        return limsJson;
    }

    @Override
    public LimsJson delEquiFile(String fileIds) {
        LimsJson limsJson=new LimsJson(1,"err");
        String[] filedId=fileIds.split(",");
        int count=0;
        for(String fid:filedId){
            count=limsEquipFileMapper.deleteByPrimaryKey(Integer.parseInt(fid));
        }
        if (count>0){
            limsJson.setCode(0);
            limsJson.setMsg("删除成功");
        }else{
            limsJson.setMsg("无法删除，请联系管理员");
        }
        return limsJson;
    }

    /** 创建作者:   王穗-+
    * 创建日期:   15:12 2020/1/8
    * 方法介绍:    根据更换周期获取下次更换时间
    * 参数说明:   [InatallTime, day]
    * @return     java.util.Date
    */
    private Date newNextInatallTime(Date InatallTime,String day){
        Calendar cal = Calendar.getInstance();
        cal.setTime(InatallTime);//设置起时间
        switch (day){
            case "0":   //每两年
                cal.add(Calendar.YEAR, 2);
                break;
            case "1":   //每天
                cal.add(Calendar.DATE, 1);
                break;
            case "2":  //每周
                cal.add(Calendar.DATE, 7);
                break;
            case "3":  //每月
                cal.add(Calendar.MONTH, 1);
                break;
            case "4":  //每季度
                cal.add(Calendar.MONTH, 3);
                break;
            case "5":  //半年
                cal.add(Calendar.MONTH, 6);
                break;
            case "6":  //每年
                cal.add(Calendar.YEAR, 1);
                break;
        }
        InatallTime=cal.getTime();
        return InatallTime;
    }
}
